﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DoeObjects
{
    public class SemesterDateHelper
    {
        /// <summary>
        /// returns the end date of the semester
        /// last friday in may (wintersemester) or last friday before christmas (summersemester)
        /// </summary>
        /// <returns></returns>
        public static DateTime getEndOfCurrentSemester()
        {
            //last friday in may:
            DateTime lastFridayInMay = new DateTime(DateTime.Now.Year, 5, 31);
            while (lastFridayInMay.DayOfWeek != DayOfWeek.Friday)
            {
                lastFridayInMay = lastFridayInMay.AddDays(-1);
            }

            //last friday before christmas:
            DateTime lastFridayBeforeChristmas = new DateTime(DateTime.Now.Year, 12, 23); //23, because otherwise it could end on christmas
            while (lastFridayBeforeChristmas.DayOfWeek != DayOfWeek.Friday)
            {
                lastFridayBeforeChristmas = lastFridayBeforeChristmas.AddDays(-1);
            }

            //get current semestertype:
            if (DateTime.Now.Date.CompareTo(lastFridayInMay) < 0)
            {
                //we are today earlier than lastFridayInMay --> the end of the current semester is lastFridayInMay
                return lastFridayInMay;
            }
            else if (DateTime.Now.Date.CompareTo(lastFridayInMay) > 0)
            {
                //we are today later than lastFridayInMay --> end of the current semester is the friday before christmas
                return lastFridayBeforeChristmas;
            }
            else// if (DateTime.Now.Date.CompareTo(lastFridayInMay) == 0)
            {
                //we are today on the lastFridayInMay --> end of semester is today :)
                return lastFridayInMay;
            }
        }
    }
}
